package com.changgou.search.controller;

import com.changgou.entity.Page;
import com.changgou.search.service.EsSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * <dl>
 * <dd>描述: ~节点业务逻辑实现</dd>
 * <dd>创建时间：  9:44 2020/4/6</dd>
 * <dd>创建人： zz</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2020/4/6      guodong       1.0        1.0 Version
 * </pre>
 * </dl>
 */
@Controller
@RequestMapping("search")
public class SearchController {

    private Logger log = LoggerFactory.getLogger(SearchController.class);
    @Autowired
    private EsSearchService searchService;

    /**
    * @Date: 9:48 2020/4/6
    * @Param: [searchMap]
    * @return: com.changgou.entity.Result
    * @Description: 用户搜索
    **/
    @GetMapping("list")
    public String search(@RequestParam Map<String,String> searchMap, Model model){
        Map<String, Object> resultMap = searchService.search(searchMap);
        //1.数据结果
        model.addAttribute("result", resultMap);
        //2.搜索数据回显
        model.addAttribute("searchMap", searchMap);
        //3.分页数据
        long total = (long) resultMap.get("total");
        int pageNum = (int) resultMap.get("pageNum");
        int pageSize = (int) resultMap.get("pageSize");
        Page page = new Page(total, pageNum, pageSize);
        model.addAttribute("page", page);
        //4.url回显
        StringBuilder url = new StringBuilder();
        url.append("/search/list");
        //7.排序url特殊处理
        StringBuilder sortUrl = new StringBuilder();
        sortUrl.append("/search/list");
        buildUrl(url, sortUrl,searchMap);
        log.info("拼接完毕的url : {}", url);
        model.addAttribute("url", url);
        model.addAttribute("sortUrl", sortUrl);

        return "search";
    }

    /**
    * @date: 9:49 2020/4/9
    * @param: [url, searchmap]
    * @return: void
    * @description:  url处理
    **/
    private void buildUrl(StringBuilder url, StringBuilder sortUrl, Map<String, String> searchMap) {
        if(!CollectionUtils.isEmpty(searchMap)){
            //http://localhost:9009/search/list?keywords=手机&brandName=华为&pageNum=2
            url.append("?");
            sortUrl.append("?");
            for (Map.Entry<String, String> entry : searchMap.entrySet()) {
                String searchKey = entry.getKey();
                String searchVal = entry.getValue();
                //5.排除pageNum
                if(!searchKey.equals("pageNum")){
                    //6.处理spec规格查询中的特殊符号  spec_版本=6G+128G ----->spec_版本=6G%2B128G
                    if(searchKey.startsWith("spec_")){
                        searchVal = searchVal.replace("+", "%2B");
                    }
                    url.append(searchKey);
                    url.append("=");
                    url.append(searchVal);
                    url.append("&");

                    if(!searchKey.equals("sortField") && !searchKey.equals("sortRule")){
                        sortUrl.append(searchKey);
                        sortUrl.append("=");
                        sortUrl.append(searchVal);
                        sortUrl.append("&");
                    }
                }
            }
            //去除最后一位 &
            url.deleteCharAt(url.length()-1);
            sortUrl.deleteCharAt(sortUrl.length()-1);
        }
    }
}
